var consultCtrl  = new XFApp.client();
$.views.converters({
	dateFormat : function(value) {
		if(!value){
			return "";
		}
		
		return  value.split(" ")[0];
	},
	scoreFormat:function(value){
		var star = "";
		if(!!value){
			for(var i = 1;i<=value;i++){
				star+="☆";
			}
		}
		return star;
	}
});
XFApp.client.prototype.consult = {
	successCode : "10000",
	page : null,
	select_index : 0,
	selectItems :[],
	urlItems : {
		queryByPageURL : basePath + "/govConsulation/queryPage",
		queryAuthCode : basePath + "/verifyCode/creator",
		queryAcceptStatus : basePath + "/govConsulation/queryAccept"
	},
	
	 loadConsult : function(divId,params,showPageCount){
			var _this = this;
	     	$.ajax({
					url:_this.urlItems.queryByPageURL,
					dataType:'json',
					type:'post',
					data:params,
					success:function(data){
						if(data.code==10000){
							$("#consulation"+_this.select_index).html($("#list_templete").render(data.result.itemList));
							_this.renderPageDiv(divId,data.result,params,showPageCount);
						}
					}
	    		 });
	     },
	     
	     renderPageDiv : function(divId,page,params,showPageCount){
	    	var _this = this;
	     	var pageDiv = '';
	     	pageDiv+='<a href="javascript:void(0);" class="ui-paging-prev" localeString="previousPage">上一页</a>';
	     	var startIndex = page.totalPage-page.pageNo>=showPageCount?page.pageNo:(page.totalPage-showPageCount>=1?page.totalPage-showPageCount:1);
	     	var pageEnd = showPageCount+page.pageNo>page.totalPage?page.totalPage:showPageCount+page.pageNo;
	     	for(var pageIndex = startIndex;pageIndex<=pageEnd;pageIndex++){
	     		var curStr = '';
	     		if(pageIndex == page.pageNo){
	     			curStr = 'ui-paging-current';
	     		}
	     		if(pageIndex==pageEnd && pageEnd!=page.totalPage){
	     			pageDiv+='<span class="ui-paging-ellipsis">...</span> <a href="javascript:void(0);" class="ui-paging-item">'+page.totalPage+'</a>';
	     		}else{
	     			pageDiv+='<a href="javascript:void(0);" class="ui-paging-item '+curStr+'">'+pageIndex+'</a>';
	     		}
	     	}
	     	pageDiv+='<a href="javascript:void(0);" class="ui-paging-next" localeString="nextPage">下一页</a>';
	     	pageDiv+='<span class="ui-paging-info"><span class="ui-paging-bold">'+page.pageNo+'/'+page.totalPage+'</span ><span localeString="page">页</span></span>';
	     	pageDiv+='<span class="ui-paging-which"><input type="text" name="some_name" value="'+page.pageNo+'"></span>';
	     	pageDiv+='<a class="ui-paging-info ui-paging-goto" href="javascript:void(0);" localeString="gotoPage">跳转</a>';
	     	$("#"+divId).html('<div class="page-ft"><div class="ui-paging">'+pageDiv+'</div></div>');
	     	$("#"+divId+" *[localeString]").each(function(){
	    		  var localeName = $(this).attr("localeString");
	    		  $(this).html(localeLangues[localeName]);
	       	});
	     	$("#"+divId+" .ui-paging-prev").on("click",function(){
	     		if(page.pageNo>1){
	     			params['pageNo']=page.pageNo-1;
	     			_this.loadConsult(divId,params,showPageCount);
	     		}
	     	});
	     	$("#"+divId+" .ui-paging-next").on("click",function(){
	     		if(page.pageNo<page.totalPage){
	     			params['pageNo']=page.pageNo+1;
	     			_this.loadConsult(divId,params,showPageCount);
	     		}
	     	});
	     	
	     	$("#"+divId+" .ui-paging-item").on("click",function(){
	     		if($(this).hasClass("ui-paging-current")){
	     			return;
	     		}
	     		params['pageNo']=$(this).text();
	 			_this.loadConsult(divId,params,showPageCount);
	     	});
	     	$("#"+divId+" .ui-paging-goto").on("click",function(){
					var goPage = $("#"+divId+" input[name='some_name']").val();
					if(!isNaN(goPage) && goPage>=1 && goPage<= page.totalPage && goPage!=page.pageNo){
						params['pageNo']=goPage;
			 			_this.loadConsult(divId,params,showPageCount);
					}else{
						$("#"+divId+" input[name='some_name']").val("");
					}
	     	});
	     },
	getParams : function(){
		 var params;
	        if(_this.select_index == 0){
	         	params = {
	        			title:$("#title").val(),
	        			consulationNo:$("#consulationNo").val(),
	        			pageSize : 5,
	        			pageNo:1
	        		}
	        }else{
	        	params = {
	        			title:$("#title").val(),
	        			consulationNo:$("#consulationNo").val(),
	        			department : _this.select_index, 
	        			pageSize : 5,
	        			pageNo:1
	        		}
	        }
		return params;
	}, 
	queryAcceptStatus : function(){
		var _this = this;
		var params = {
				consulationNo:$("#sl_consulationNo").val(),
				verifyCode:$("#verifyCode").val()
		}
		$.ajax({
			url:_this.urlItems.queryAcceptStatus,
			dataType:'json',
			type:'post',
			data:params,
			success:function(data){
				if(data.code==10000){
					window.location = "govConsulation/view?consulationNo="+data.result;
				}else{
					webDialogUtil.simpleAlert(data.msg, "温馨提示");
					_this.changeCode();
				}
			}
		 });
	},
	changeCode : function(){
		$("#img_auth_code").attr("src",_this.urlItems.queryAuthCode+"?time="+new Date().getTime());
     	
	},
	event : function(){
		_this = this;
		
        // 切换卡
        var $tab_li = $('.ui-tab-items .ui-tab-item');
        $tab_li.hover(function(){
            $(this).addClass('ui-tab-item-current').siblings().removeClass('ui-tab-item-current');
            _this.select_index = $tab_li.index(this);
            $('.ui-switchable-content .ui-switchable-panel').eq(_this.select_index).addClass('ui-switchable-pane-current').siblings().removeClass('ui-switchable-pane-current');
            
            if(_this.selectItems.indexOf(_this.select_index)==-1){
            	var params = _this.getParams();
            	_this.selectItems.push(_this.select_index);
            	_this.loadConsult("page_div",params,7);
            }
        });
        
        if(!!departmentIndex){
        	$tab_li.eq(departmentIndex).addClass('ui-tab-item-current').siblings().removeClass('ui-tab-item-current');
        	 _this.select_index = departmentIndex;
        	 $('.ui-switchable-content .ui-switchable-panel').eq(_this.select_index).addClass('ui-switchable-pane-current').siblings().removeClass('ui-switchable-pane-current');
             
             if(_this.selectItems.indexOf(_this.select_index)==-1){
             	var params = _this.getParams();
             	_this.selectItems.push(_this.select_index);
             	_this.loadConsult("page_div",params,7);
             }
        }else{
        	consultCtrl.consult.loadConsult("page_div",_this.getParams(),7);
        }
		
		$("#query_btn").on("click",function(){
			var params = _this.getParams();
			_this.loadConsult("page_div",params,7);
		});
		
		$("#sl_query_btn").on("click",function(){
			_this.queryAcceptStatus();
		});
		
		$("#img_auth_code").on("click",function(){
			_this.changeCode();
		});
		$("#ch_auth_code").on("click",function(){
			_this.changeCode();	
		});
	}
};
$(function(){
	consultCtrl.consult.event();
});